// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   FileOperator.java

package com.vandagroup.common.util.file;

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

// Referenced classes of package com.vandagroup.common.util.file:
//            FileData

public class FileOperator
{

    private static String OVERWRITE_WHEN_EXIST = "Y";
    private static int READ_BUFFER_SIZE = 4096;
    private int line_buffer_size;

    public FileOperator()
    {
        line_buffer_size = 1024;
    }

    public String read(String filePath)
        throws Exception
    {
        String fileContent = null;
        try
        {
            FileInputStream fstream = new FileInputStream(filePath);
            DataInputStream in;
            for(in = new DataInputStream(fstream); in.available() != 0;)
                fileContent = fileContent + in.readLine();

            in.close();
        }
        catch(IOException e)
        {
            System.err.println("FileOperator.read() IO error");
            throw e;
        }
        catch(Exception e)
        {
            System.err.println("FileOperator.read() error");
            throw e;
        }
        return fileContent;
    }

    public static void remove(String filePath)
        throws Exception
    {
        File aFile = new File(filePath);
        if(aFile.exists())
            aFile.delete();
    }

    public void copy(String from_name, String to_name)
        throws IOException
    {
        File from_file = new File(from_name);
        File to_file = new File(to_name);
        if(!from_file.exists())
            abort("no such source file: " + from_name);
        if(!from_file.isFile())
            abort("can't copy directory: " + from_name);
        if(!from_file.canRead())
            abort("source file is unreadable: " + from_name);
        System.out.println("file is exist = " + from_file.exists());
        if(to_file.isDirectory())
            to_file = new File(to_file, from_file.getName());
        if(to_file.exists())
        {
            if(!to_file.canWrite())
                abort("destination file is unwriteable: " + to_name);
            if(!OVERWRITE_WHEN_EXIST.equals("Y") && !OVERWRITE_WHEN_EXIST.equals("y"))
                abort("existing file was not overwritten.");
        } else
        {
            String parent = to_file.getParent();
            if(parent == null)
                parent = System.getProperty("user.dir");
            File dir = new File(parent);
            if(!dir.exists())
                abort("destination directory doesn't exist: " + parent);
            if(dir.isFile())
                abort("destination is not a directory: " + parent);
            if(!dir.canWrite())
                abort("destination directory is unwriteable: " + parent);
        }
        FileInputStream from = null;
        FileOutputStream to = null;
        try
        {
            from = new FileInputStream(from_file);
            to = new FileOutputStream(to_file);
            byte buffer[] = new byte[READ_BUFFER_SIZE];
            int bytes_read;
            while((bytes_read = from.read(buffer)) != -1) 
                to.write(buffer, 0, bytes_read);
        }
        finally
        {
            if(from != null)
                try
                {
                    from.close();
                }
                catch(IOException ioexception) { }
            if(to != null)
                try
                {
                    to.close();
                }
                catch(IOException ioexception1) { }
            System.out.println("file is exist = " + from_file.exists());
        }
        return;
    }

    private static void abort(String msg)
        throws IOException
    {
        throw new IOException("FileCopy: " + msg);
    }

    public FileData readToArray(String filePath, String itemSeparator)
        throws Exception
    {
        FileData aFileData;
        String fileLine = null;
        String item = null;
        StringTokenizer tokenizer = null;
        BufferedReader in = null;
        FileReader fstream = null;
        aFileData = null;
        try
        {
            fstream = new FileReader(filePath);
            in = new BufferedReader(fstream);
            aFileData = new FileData();
            while((fileLine = in.readLine()) != null) 
            {
                aFileData.addNewLine();
                for(tokenizer = new StringTokenizer(fileLine, itemSeparator); tokenizer.hasMoreTokens(); aFileData.addLineData(item))
                    item = tokenizer.nextToken();

            }
            ArrayList line = null;
            for(int i = 0; i < aFileData.getLineCount(); i++)
                line = aFileData.getLineData(i);

        }
        catch(IOException e)
        {
            System.err.println("FileOperator.read() IO error");
            throw e;
        }
        catch(Exception e)
        {
            System.err.println("FileOperator.read() error");
            throw e;
        }
        finally
        {
            fstream.close();
            in.close();
        }
        return aFileData;
    }

    public FileData readToArray(String filePath, int fileConfig[])
        throws Exception
    {
        FileData aFileData;
        String fileLine = null;
        String item = null;
        StringTokenizer tokenizer = null;
        BufferedReader in = null;
        FileReader fstream = null;
        aFileData = null;
        int currentPos = 0;
        int endPos = 0;
        try
        {
            fstream = new FileReader(filePath);
            in = new BufferedReader(fstream);
            aFileData = new FileData();
            while((fileLine = in.readLine()) != null) 
            {
                aFileData.addNewLine();
                currentPos = 0;
                for(int i = 0; i < fileConfig.length; i++)
                {
                    if((endPos = currentPos + fileConfig[i]) > fileLine.length())
                        endPos = fileLine.length();
                    item = fileLine.substring(currentPos, endPos).trim();
                    aFileData.addLineData(item);
                    currentPos += fileConfig[i];
                }

            }
            ArrayList line = null;
            for(int i = 0; i < aFileData.getLineCount(); i++)
            {
                System.out.print("line" + i + ":");
                line = aFileData.getLineData(i);
                for(int j = 0; j < line.size(); j++)
                    System.out.print(line.get(j));

                System.out.println();
            }

        }
        catch(IOException e)
        {
            System.err.println("FileOperator.read() IO error");
            throw e;
        }
        catch(Exception e)
        {
            System.err.println("FileOperator.read() error");
            throw e;
        }
        finally
        {
            fstream.close();
            in.close();
        }
        return aFileData;
    }

    public void setLineBufferSize(int size)
    {
        line_buffer_size = size;
    }

}
